package org.groupg.practice.configuration.validator;

import org.apache.commons.configuration2.Configuration;
import org.apache.commons.configuration2.ex.ConfigurationException;

import java.io.File;

/**
 * 文件路径校验器：检查配置项指向的文件是否存在
 */
public class FilePathValidator implements ConfigurationValidator {
    private final String key;

    public FilePathValidator(String key) {
        this.key = key;
    }

    @Override
    public void validate(Configuration config) throws ConfigurationException {
        String path = config.getString(key);
        if (path == null) {
            throw new ConfigurationException("配置项缺失: " + key);
        }
        File file = new File(path);
        if (!file.exists()) {
            throw new ConfigurationException("文件不存在: " + path);
        }
    }
}